<template>
  <div>
    <z-header>登录</z-header>
    <section class="page">
      <div class="login-page">
        <div class="login-wrap">
          <div class="login-text">
            <van-field
              v-model="username"
              icon-prefix="iconfont"
              right-icon="guanbi"
              placeholder="用户名/邮箱/已验证手机"
              @click-right-icon="clearUserText"
            />
          </div>
          <div class="login-text">
            <van-field
              v-model="password"
              icon-prefix="iconfont"
              right-icon="guanbi"
              placeholder="请输入密码"
              type="password"
              ref="passwordText"
              @click-right-icon="clearPwdText"
              v-if="!passwordType"
            />
            <van-field
              v-model="password"
              icon-prefix="iconfont"
              right-icon="guanbi"
              placeholder="请输入密码"
              type="text"
              ref="passwordText"
              @click-right-icon="clearPwdText"
              v-else
            />
            <div class="forget">
              <i
                class="iconfont iconfont-biyanjing eye"
                @click="changeType"
                v-if="!passwordType"
              ></i>
              <i
                class="iconfont iconfont-yanjing eye"
                @click="changeType"
                v-else
              ></i>
              <span>忘记密码</span>
            </div>
          </div>
        </div>

        <button
          class="login-button"
          :class="{ active: username.trim() && password.trim() }"
          @click="submitLogin"
        >
          登录
        </button>
        <div class="quick-nav">
          <span class="register-button">快速注册</span>
        </div>
        <div class="other-login">
          <div class="other-head">
            <i></i>
            <span>其它登录方式</span>
            <i></i>
          </div>
          <div class="other-con">
            <div class="login-icon" @click="handQQ">
              <i class="iconfont iconfont-qq"></i>
              <span>QQ</span>
            </div>
            <div class="login-icon">
              <i class="iconfont iconfont-weixin"></i>
              <span>微信</span>
            </div>
          </div>
          <p>登录即代表您已同意 知心购隐私政策</p>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import zHeader from "@/components/common/z-header.vue";
import { login } from "../../api/api";
import { mapMutations } from "vuex";
import { Toast } from "vant";
export default {
  data() {
    return {
      username: "",
      password: "",
      errMsg: "",
      passwordType: false,
    };
  },
  components: { zHeader },
  methods: {
    ...mapMutations(["setToken"]),
    clearUserText() {
      // 数组关联法
      this["username"] = "";
    },
    clearPwdText() {
      this["password"] = "";
    },
    changeType() {
      this.passwordType = !this.passwordType;
    },
    submitLogin() {
      login({ username: this.username, password: this.password }, (res) => {
        if (res.status === 0) {
          this.setToken({ token: res.data.token });
          // 登录完成 触发获取用户信息
          this.$store.dispatch("getInfo");
          // 登录的时候 获取购物车列表数据
          this.$store.dispatch("getCartData");
          this.$router.replace("/user");
        } else {
          this.errMsg = res.msg ? res.msg : "登录失败";
          Toast.fail(this.errMsg);
        }
      });
    },
    handQQ() {
      /* 
        QQ SDK 提供的 授权方法
        新打开一个页面 进行授权
      */
     console.log('---');
      // this.$QQ.login.showPopup({
      //   appId: "101850878",
      //   redirectURI: "http://localhost:8080/proxy",
      // });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/common/style/mixin.scss";
.page {
  width: 100%;
  padding: 63px 36px 0;
  box-sizing: border-box;

  .login-page {
    margin-top: 43px;

    .login-wrap {
      width: 100%;

      .login-text {
        display: flex;
        justify-content: space-between;
        padding: 14px 0;
        border-bottom: 1px solid #dcdcdc;

        .iconfont {
          color: #cccccc;

          &.eye {
            padding: 0 30px;
            font-size: 25px;
            border-right: 1px solid #dcdcdc;
          }
        }

        .forget {
          display: flex;
          align-items: center;

          span {
            font-size: 21px;
            line-height: 43px;
          }
        }
      }
    }

    .login-button {
      width: 100%;
      height: 72px;
      margin-top: 72px;
      background: rgba(246, 53, 21, 0.5);
      font-size: 23px;
      color: #fff;
      border-radius: 72px;

      &.active {
        background: rgb(246, 53, 21);
      }
    }

    .quick-nav {
      padding: 28px 0;
      text-align: right;

      .register-button {
        color: #999;
        font-size: 20px;
      }
    }

    .other-login {
      margin-top: 72px;

      .other-head {
        display: flex;
        justify-content: space-between;

        i {
          flex: 1;
          height: 1px;
          margin-top: 13px;
          background: #dcdcdc;
        }

        span {
          flex: 1;
          text-align: center;
          color: #dcdcdc;
          font-size: 20px;
        }
      }

      .other-con {
        display: flex;
        width: 230px;
        height: 157px;
        margin: 0 auto;
        padding: 28px 0;
      }

      p {
        margin-top: 14px;
        color: #999;
        text-align: center;
      }
    }

    .other-con {
      display: flex;
      justify-content: space-between;
      width: 320px;
      padding: 40px 0;
      margin: 0 auto;

      .login-icon {
        display: flex;
        flex-direction: column;
        width: 96px;
        // margin: 0 30px;
        text-align: center;

        .iconfont {
          width: 100%;
          height: 96px;
          line-height: 96px;
          font-size: 60px;
          color: #15b8f5;
          background: #e7f7fe;
          @include borderRadius(50%);

          &.iconfont-weixin {
            font-size: 50px;
            color: #09bb07;
            background: #e6f8e6;
          }
        }

        span {
          padding-top: 20px;
          color: #999;
        }
      }
    }
  }
}

.login-text:nth-child(2) {
  .van-cell {
    width: 70%;
  }
}

// 交集选择器
.van-cell.van-field::after {
  border: none;
}
</style>